﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using ZD_DataModel;

namespace ZD_Utility
{
    public class ZD_GetModelCache
    {

        //默认构造函数
        public ZD_GetModelCache()
        {

        }

        #region 根据Model获取ZD_TabNameAttribute特性类（映射数据库表相关信息）
        /// <summary>
        /// 根据Model获取ZD_TabNameAttribute特性类（映射数据库表相关信息）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static ZD_TabNameAttribute GetTabNameAttr(object Model)
        {
            Type type = Model.GetType();
            object obj = ZD_DataCache.GetCache(type.FullName + ZD_DataCacheKey.ModelTabAttribute);
            if (obj == null)
            {
                var AttributeArr = type.GetCustomAttributes(typeof(ZD_TabNameAttribute), false);
                if (AttributeArr.Length > 0)
                {
                    obj = AttributeArr[0];
                    ZD_DataCache.InsertCacheExpired(type.FullName + ZD_DataCacheKey.ModelTabAttribute, obj, null);
                }
                else
                {
                    throw new Exception("实体类没有ZD_TabNameAttribute特性，无法操作！");
                }
            }
            return (ZD_TabNameAttribute)obj;
        }
        #endregion

        #region 根据Model获取PropertyInfo
        /// <summary>
        /// 根据Model获取PropertyInfo
        /// 先检测缓存中是否存在，不存在则重新通过反射获取
        /// </summary>
        /// <param name="Model">Model对象</param>
        /// <returns></returns>
        public static PropertyInfo[] GetPropertyInfoByModel(object Model)
        {
            Type type = Model.GetType();
            object obj = ZD_DataCache.GetCache(type.FullName + ZD_DataCacheKey.ModelPropertyInfo);
            if (obj == null)
            {
                obj = Model.GetType().GetProperties();
                ZD_DataCache.InsertCacheExpired(type.FullName + ZD_DataCacheKey.ModelPropertyInfo, obj, null);
            }
            return (PropertyInfo[])obj;
        }
        #endregion

        #region 根据Model获取PropertyType
        /// <summary>
        /// 根据Model获取PropertyType
        /// 先检测缓存中是否存在，不存在则重新通过反射获取
        /// </summary>
        /// <param name="Model">Model对象</param>
        /// <returns></returns>
        public static Dictionary<string, string> GetPropertyTypeByModel(object Model)
        {
            Type type = Model.GetType();
            var List = (Dictionary<string, string>)ZD_DataCache.GetCache(type.FullName + ZD_DataCacheKey.ModelPropertyType);
            if (List == null)
            {
                List = new Dictionary<string, string>();
                foreach (PropertyInfo item in GetPropertyInfoByModel(Model))
                {
                    #region 添加每个属性的对应类型
                    switch (item.PropertyType.ToString())
                    {
                        case "System.Nullable`1[System.Int32]":
                            List.Add(item.Name.ToLower(), "int");
                            break;
                        case "System.Guid":
                            List.Add(item.Name.ToLower(), "guid");
                            break;
                        case "System.String":
                            List.Add(item.Name.ToLower(), "string");
                            break;
                        case "System.Nullable`1[System.Single]":
                            List.Add(item.Name.ToLower(), "int");
                            break;
                        case "System.Nullable`1[System.DateTime]":
                            List.Add(item.Name.ToLower(), "string");
                            break;
                        default:
                            break;
                    }
                    #endregion
                }
                ZD_DataCache.InsertCacheExpired(type.FullName + ZD_DataCacheKey.ModelPropertyType, List, null);
            }
            return List;
        }
        #endregion

        #region 根据Model获取PropertyNames
        /// <summary>
        /// 根据Model获取Property属性名集合
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public static List<string> GetPropertyKeys(object Model)
        {
            Type type = Model.GetType();
            var List = (List<string>)ZD_DataCache.GetCache(type.FullName + ZD_DataCacheKey.ModelPropertyNames);
            if (List == null)
            {
                List = new List<string>();
                foreach (PropertyInfo item in GetPropertyInfoByModel(Model))
                {
                    List.Add(item.Name);
                }
                ZD_DataCache.InsertCacheExpired(type.FullName + ZD_DataCacheKey.ModelPropertyNames, List, null);
            }
            return List;
        }
        #endregion
    }
}
